package com.wlps.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlps.bo.*;
import com.wlps.entity.*;
import com.wlps.mapper.SysUserMapper;
import com.wlps.result.ResultPageVo;
import com.wlps.result.ResultUtils;
import com.wlps.result.ResultVo;
import com.wlps.service.*;
import com.wlps.service.function.DispatchingMangementCenter;
import com.wlps.utils.PageUtils;
import com.wlps.vo.*;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags="调度中心管理模块")
@RestController
@RequestMapping("/api/dispatching_management/")
public class DispatchingManagementController {

    @Resource
    DispatchingMangementCenter dispatchingMangementCenter;

    @GetMapping("query_all_order")
    @ApiOperation("查询所有调度订单")
    @Transactional
    public ResultPageVo<DispatchingManagementVo> queryAllDispatchingManagement(){
        List<DispatchingManagementVo> dispatchingManagementVos = dispatchingMangementCenter.queryAllDispatchManagement();
        return PageUtils.getPageInfo(dispatchingManagementVos);
    }
    @PostMapping("query_order_on_require")
    @ApiOperation("按条件查询调度订单")
    public ResultVo<List<DispatchingManagementVo>>queryDispatchingManagementOnRequire(@RequestBody QueryComposeBo queryBo){
        List<DispatchingManagementVo> dispatchingManagementVos = dispatchingMangementCenter.queryDispatchManagementByRequire(queryBo);
        if(dispatchingManagementVos==null)
            throw new RuntimeException("查询信息为空,请确定查询条件是否出错");
        return ResultUtils.success("查询成功",dispatchingManagementVos);
    }

    @GetMapping("query_order_by_status_type/{orderStatus}/{orderType}")
    @ApiOperation("通过订单状态和订单类型查询订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderStatus",value = "订单状态"),
            @ApiImplicitParam(name="orderType",value = "订单类型")
    })
    public ResultVo<DispatchingManagementVo> queryDispatchingManagementByStatusAndType(@PathVariable String orderStatus,@PathVariable String orderType){
        List<DispatchingManagementVo>list =new ArrayList<>();
        return ResultUtils.success("查询成功",list);
    }
    @GetMapping("query_send_user")
    @ApiOperation("查询有哪些员工")
    public ResultVo<String> querySysSendUse(){
        List<SenderVo> senderVos = dispatchingMangementCenter.querySender();
        return ResultUtils.success("查询成功",senderVos);
    }
    @GetMapping("add_receipt/{id}/{status}/{satifaction}/{remark}")
    @ApiOperation("回执录入")
    public ResultVo addReceipt(@PathVariable long id, @PathVariable Integer status,@PathVariable String satifaction,@PathVariable String remark){
        if(dispatchingMangementCenter.updateRecepitMissionList(id,status,satifaction,remark))
        return ResultUtils.success("回执录入成功");
        return ResultUtils.error();
    }

    @GetMapping("queryDispat_by_id/{id}")
    @ApiOperation("根据商品id查询调度信息")
    public ResultVo<List<DispatchingManagementVo>> queryOneDispatchManagement(@PathVariable long id){
        DispatchingManagementVo dispatchingManagementVo = dispatchingMangementCenter.queryOneDispatchManagement(id);
        ArrayList<DispatchingManagementVo> objects = new ArrayList<>();
        objects.add(dispatchingManagementVo);
        if(dispatchingManagementVo!=null)
            return ResultUtils.success("查询成功",objects);
        else
            return ResultUtils.error();
    }
    @ApiOperation("根据id查询调度订单详细信息")
    @GetMapping("query_detail_order_by_id/{id}")
    public ResultVo<List<OrderCommodityVo>> queryDetailOrderById(@PathVariable long id){
        OrderCommodityVo orderCommodityVos = dispatchingMangementCenter.queryDetailOrderById(id);
        List<OrderCommodityVo> objects = new ArrayList<>();
        objects.add(orderCommodityVos);
        if(orderCommodityVos!=null)
            return ResultUtils.success("查询成功",objects);
        return ResultUtils.error();
    }
    @ApiOperation("根据id订单信息")
    @GetMapping("query_order_by_id/{id}")
    public ResultVo<Order> queryOrderById(@PathVariable long id) {
        Order order = dispatchingMangementCenter.queryOrderById(id);
        return ResultUtils.success("查询成功",order);

    }
    @GetMapping("updateOrder/{orderId}/{status}")
    @ApiOperation("根据订单id更新订单状态")
    public ResultVo<Order> updateStatus(@PathVariable Integer orderId, @PathVariable Integer status){
        return ResultUtils.success("更新成功",dispatchingMangementCenter.updateStatus(orderId, status));
    }
    @GetMapping("generatorMissionList/{userId}/{orderId}/{serviceMoney}/{remark}")
    @ApiOperation("更新任务单")
    public ResultVo<MissionList> generatorMissionList(@PathVariable long userId, @PathVariable long orderId,@PathVariable Double serviceMoney,@PathVariable String remark){
        return ResultUtils.success("更新订单成功",dispatchingMangementCenter.updateMissionList(userId, orderId, serviceMoney, remark));
    }
    @GetMapping("generatorMissionList/{orderId}")
    @ApiOperation("生成任务单")
    public ResultVo<MissionList> generatorMissionList(@PathVariable long orderId){
        return ResultUtils.success("生成订单成功",dispatchingMangementCenter.generatorMissionList(orderId));
    }
    @GetMapping("query_recepit_by_id/{id}")
    @ApiOperation("根据任务单id查询回执")
    public ResultVo<ReceiptVo> printReceiptVo(@PathVariable long id){
        ReceiptVo receiptVo = dispatchingMangementCenter.printReceiptVo(id);
        return ResultUtils.success("查询成功",receiptVo);
    }
    @GetMapping("query_missionList/{id}")
    @ApiOperation("id:功能 0:查询所有没有配送的订单 1:查询所有配送订单")
    public ResultVo<List<MissionList>> queryMissionRequire(@PathVariable long id){
        IMissionListService missionListService = dispatchingMangementCenter.getMissionListService();
        LambdaQueryWrapper<MissionList> eq = new LambdaQueryWrapper<MissionList>().eq(id == 0, MissionList::getUserId, -1L).eq(id == 1, MissionList::getUserId, id != 1L);
        List<MissionList> list = missionListService.list(eq);
        return ResultUtils.success("查询成功",list);
    }
    @GetMapping("delete_order/{id}")
    public ResultVo deleteOrder(@PathVariable long id){
        if (dispatchingMangementCenter.deleteOrder(id)) {
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error();
    }
    @ApiOperation("缴费查询")
    @PostMapping("get_commodity_money")
    public ResultVo<GetMoneyVo> getMoney(@RequestBody GetMoneyBo getMoneyBo){
        ICommodityService commodityService = dispatchingMangementCenter.getOrderCenter().getCommodityService();
        Commodity one = commodityService.getOne(new LambdaQueryWrapper<Commodity>().eq(Commodity::getProductName, getMoneyBo.getProductName()));
        if(one==null)
            throw new RuntimeException("不存在该名称的商品");
        ICommodityOrderService commodityOrderService = dispatchingMangementCenter.getCommodityOrderService();
        LambdaQueryWrapper<CommodityOrder> wrapper = new LambdaQueryWrapper<CommodityOrder>().eq(CommodityOrder::getCommodityId, one.getId()).ge(getMoneyBo.getStartTime() != null, CommodityOrder::getCreateTime, getMoneyBo.getStartTime())
                .lt(getMoneyBo.getEndTime() != null, CommodityOrder::getCreateTime, getMoneyBo.getEndTime());
        List<CommodityOrder> list = commodityOrderService.list(wrapper);
        List<Long> collect = list.stream().map(CommodityOrder::getOrderId).collect(Collectors.toList());
        OrderCenter orderCenter = dispatchingMangementCenter.getOrderCenter();
        Collection<Order> orders = orderCenter.getOrderService().listByIds(collect);
        List<Long> exit = orders.stream().filter(data -> data.getOrderStatus() == 5).map(Order::getId).collect(Collectors.toList());
        List<CommodityOrder> list1;
        list1 = dispatchingMangementCenter.getCommodityOrderService().list(new LambdaQueryWrapper<CommodityOrder>().in(exit.size()!=0,CommodityOrder::getOrderId, exit));

        GetMoneyVo getMoneyVo = new GetMoneyVo();
        for (CommodityOrder data : list) {
            getMoneyVo.setNumber(data.getAmountOfGoods());
            getMoneyVo.setSumMoney(data.getCommodityId()*one.getUnitPrice()*one.getDiscount());
        }
        if(list.size()==0) getMoneyVo.setExitMoney(0.0);
        else{
            list1.forEach(data->{
                getMoneyVo.setExitMoney(data.getCommodityId()*one.getUnitPrice()*one.getDiscount());
            });
        }
        getMoneyVo.setInfo("退货单数量为"+list1.size());
        return ResultUtils.success("查询成功",getMoneyVo);
    }
    @GetMapping("query_all_missionList")
    @ApiOperation("查询所有任务单")
    public ResultVo queryAllMissionList(){
        IMissionListService missionListService = dispatchingMangementCenter.getMissionListService();
        List<MissionList> list = missionListService.list();
        List<Long> collect = list.stream().map(MissionList::getOrderId).collect(Collectors.toList());
        Collection<Order> orders = dispatchingMangementCenter.getOrderCenter().getOrderService().listByIds(collect);
        ArrayList<MissionListVo> missionListVos = new ArrayList<>();
        Iterator<Order> iterator = orders.iterator();
        list.forEach(data->{
            MissionListVo missionListVo = new MissionListVo();
            Order order=null;
            if(iterator.hasNext()){
                order=iterator.next();
                missionListVo.setOrderStatus(order.getOrderStatus());
            }
            BeanUtils.copyProperties(data,missionListVo);
            missionListVos.add(missionListVo);
        });
        return ResultUtils.success("查询成功",missionListVos);
    }

    @Autowired
    ISysUserService userService;
    @GetMapping("/getSenderList")
    public ResultVo<List<SenderRoleUserVo>> getSenderList(){
       return userService.getSenderRoleUserVo();
    }

    @GetMapping("query_mission_by_id/{id}")
    @ApiOperation("根据id查询任务单")
    public ResultVo queryMissionList(@PathVariable long id){
        IMissionListService missionListService = dispatchingMangementCenter.getMissionListService();
        MissionList byId = missionListService.getById(id);
        ArrayList<MissionList> missionLists = new ArrayList<>();
        missionLists.add(byId);
        if(byId==null)
            throw new RuntimeException("不存在该任务单");
        return ResultUtils.success("查询成功",missionLists);
    }
}
